package top.z.orm.handle.osql.util;

import top.z.orm.handle.osql.core.HoneyConfig;
import top.z.orm.handle.osql.core.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    private static SimpleDateFormat getSimpleDateFormat() {
        SimpleDateFormat defaultFormat = null;
        String           dateFormatStr = HoneyConfig.getHoneyConfig().getDateFormat();
        if (dateFormatStr != null && !"".equals(dateFormatStr.trim())) {
            try {
                defaultFormat = new SimpleDateFormat(dateFormatStr);
            } catch (Exception e) {
                Logger.warn("In DateUtil: it is error date format String :" + dateFormatStr);
                defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
        } else {
            defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        return defaultFormat;
    }

    public static String currentDate() {
        return getSimpleDateFormat().format(new Date());
    }

    public static String currentDate(String formatStr) {
        SimpleDateFormat format = null;
        if (formatStr == null || "".trim().equals(formatStr))
            format = getSimpleDateFormat();
        else
            format = new SimpleDateFormat(formatStr);
        return format.format(new Date());
    }
}